from CvPythonExtensions import *

import GerikSpells
from GerikSpells import GerikSpellBase
from GSGroupPolicies import GSAll as GSGroup
from GSHandlerPolicies import GSTargetting as GSHandler
from GSResultsPolicies import GSMission as GSResults

from GSTargetInfoPolicies import GSSinglePlotTarget
from CivcraftPolicies import CivcraftSpecificUnitTargetPolicy as GSSpecificUnitTarget

from CivcraftPolicies import CivcraftSpellPlotGraphicsPolicy as GSPlot
from GSGraphicsPolicies import GSPieMenu

import GerikSpellsUtils

from CivcraftSCUtils import checkAndRemoveEnergy
from CivcraftSCUtils import showNotEnoughEnergyMsg
from CivcraftSCUtils import getEnergyUsageString
from CivcraftSCUtils import isHallucination
import CivcraftSCCombat

gc = CyGlobalContext()
localtext = CyTranslator()

class BattlecruiserBase(GerikSpellBase, GSGroup, GSHandler):
    def __init__(self):
        GerikSpellBase.__init__(self)
        GSGroup.__init__(self)
        GSHandler.__init__(self)


        self.m_iBattlecruiserType = gc.getInfoTypeForString("UNIT_BATTLECRUISER")
            
    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        if not pCastingUnit.getUnitType() == self.m_iBattlecruiserType:
            return False

        if isHallucination(pCastingUnit):
            return False

        # Show this for all Battlecruisers
        if bVisible:
            return True

        return True

class Yamato(BattlecruiserBase, GSResults):
    def __init__(self):
        BattlecruiserBase.__init__(self)

        # Constants
        self.m_iYamatoEnergyCost = 150
        self.m_iYamatoRange = 5
        self.m_iYamatoDamage = 260

        self.m_iResearchNeeded = gc.getInfoTypeForString("PROJECT_TERRAN_BATTLECRUISER_YAMATO")
        self.m_iResearchBuildingToBuildAt = gc.getProjectInfo(self.m_iResearchNeeded).getBuildingToBuildAt()
        self.m_szNotValidText = localtext.getText( "TXT_KEY_CIVCRAFT_REQUIRES_RESEARCH_AT", (gc.getProjectInfo(self.m_iResearchNeeded).getDescription(),
                                                                                             gc.getBuildingInfo(self.m_iResearchBuildingToBuildAt).getDescription()) )

        # Policies
        pSpecificUnitTargetPolicy = GSSpecificUnitTarget(self.canTargetUnit)
        GSResults.__init__(self, pSpecificUnitTargetPolicy)

    def getHelpText(self, pGroup, pPlot):
        szText = getEnergyUsageString(self.m_iYamatoEnergyCost)

        bValid = self.canGroupCast(pGroup, pPlot, False)
        if not bValid:
            szText += self.m_szNotValidText
        else:
            szText += gc.getSpellInfo(self.getSpellNumber()).getHelp()

        return szText

    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        if not BattlecruiserBase.canUnitCast(self, pCastingUnit, pPlot, bVisible):
            return False

        if bVisible:
            return True

        # Check if they have the research
        if not gc.getPlayer( pCastingUnit.getOwner() ).hasBuiltProject( self.m_iResearchNeeded ):
            return False

        return True

    def canTargetPlot(self, pCastingGroup, pCastingPlot, pTargetPlot, bVisible):
        return True

    def canTargetUnit(self, pCastingGroup, pCastingPlot, pTargetUnit, bVisible):
        if bVisible:
            return True

        if pTargetUnit.isInvulnerable():
            return False

        return True

    def getRangePlotList(self, pCastingPlot):
        return GerikSpellsUtils.getRangePlotDistanceList(pCastingPlot, self.m_iYamatoRange)

    def createTargettingStageList(self, bAlt, bShift, bCtrl):
        pStageList = []

        pPlotTargetPolicy = GSSinglePlotTarget(self.canTargetPlot)
        pPlotTargetPolicy.setRangeMakerCallback(self.getRangePlotList)
        pPlotTargetPolicy.setCheckRange(True)

        pPlotGraphicsPolicy = GSPlot()
        pPlotGraphicsPolicy.setShowRangeGraphics(True)
        pPlotGraphicsPolicy.setCursorType("CURSOR_SINGLE_ATTACK")
        
        pStageList.append( (pPlotTargetPolicy, pPlotGraphicsPolicy) )

        pUnitTargetPolicy = GSSpecificUnitTarget(self.canTargetUnit)
        pUnitTargetPolicy.setInvalidTargetDescriptionCallback(self.getInvalidTargetText)
        pUnitMenuPolicy = GSPieMenu()
        pStageList.append( (pUnitTargetPolicy, pUnitMenuPolicy) )

        return pStageList

    def getInvalidTargetText(self, pTargetUnit, bStrategyText):
        szText = u""
        pCastingGroup = self.getCastingGroup()
        if pCastingGroup.getOwner() == pTargetUnit.getOwner():
            szText += u"\n"
            szText += localtext.getText("TXT_KEY_INVALID_TARGET_SAME_TEAM", ())

        if self.m_iYamatoRange < plotDistance(pCastingGroup.getHeadUnit().getX(), pCastingGroup.getHeadUnit().getY(), pTargetUnit.getX(), pTargetUnit.getY()):
            szText += u"\n"
            szText += localtext.getText("TXT_KEY_INVALID_TARGET_RANGE", ())

        if pTargetUnit.isInvulnerable():
            szText += u"\n"
            szText += localtext.getText("TXT_KEY_INVALID_TARGET_UNIT_INULNERABLE", ())

        return szText

    def startSpellResults(self, pCastingGroup, pTargetInfo):
        bShowNotEnoughEnergy = False
        
        iPlayer = pCastingGroup.getOwner()
        for pUnit in pCastingGroup.getUnits():
            if checkAndRemoveEnergy(pUnit, self.m_iYamatoEnergyCost):
                print pTargetInfo.getTarget()
                for pLoopUnit in pTargetInfo.getAffectedTargetList(pCastingGroup, pCastingGroup.plot()):
                    if not pLoopUnit.isDead():
                        iFinalDamage = CivcraftSCCombat.getFinalDamageAfterModifiers(self.m_iYamatoDamage, True, False, pLoopUnit)
                        pLoopUnit.doDamage(iFinalDamage, pCastingGroup.getOwner())
            else:
                bShowNotEnoughEnergy = True

        if bShowNotEnoughEnergy:
            showNotEnoughEnergyMsg(iPlayer)
                    
            
            
